import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import javax.swing.JOptionPane;

/**
 * This class provides an extremely rudimentary user interface.
 * The main purpose is to permit students to program simple user
 * interaction without having to know about complicated stuff like
 * importing Java library classes or handling exceptions.
 * 
 * @Gene Stark
 * @version January 4, 2004
 */
public class Dialog
{
    /**
     * Instance variable telling whether pop-up GUI dialogs or line-oriented
     * input is to be used.
     */
    private boolean useGUI = false;
    
    /**
     * Reader for getting console input from the user.
     */
    private BufferedReader reader =
        new BufferedReader(new InputStreamReader(System.in));
    
    /**
     * Constructor for objects of class Dialog.
     */
    public Dialog()
    {
    }

    /**
     * Display a simple message for the user.
     * 
     * @param  msg  The message to display.
     */
    public void showMessage(String msg)
    {
        if(useGUI)
            JOptionPane.showMessageDialog(null, msg);
        else
            System.out.println(msg);                
    }
    
    /**
     * Prompt the user to input a string.
     * 
     * @param  msg  The prompt for the user.
     * @return the string input by the user, or null if the user cancelled
     * the dialog.
     */
    public String promptForInput(String msg)
    {
        if(useGUI)
            return(JOptionPane.showInputDialog(msg));
        else {
            System.out.print(msg);
            try {
                return(reader.readLine());
            } catch(IOException x) { return(null); }
        }
    }
    
    /**
     * Switch between console and GUI interaction.
     * 
     * @param useGUI  If true, use GUI dialogs, otherwise use console I/O.
     */
    public void setGUI(boolean useGUI) {
        this.useGUI = useGUI;
    }
}
